#region (c) 2014 Nguyễn, M. Hải - All rights reserved
/*
BLADE VIEW ENGINE

The MIT License (MIT)
Copyright (c) 2014 Nguyễn, M. Hải

Permission is hereby granted, free of charge, to any person obtaining a copy of this 
software and associated documentation files (the "Software"), to deal in the Software 
without restriction, including without limitation the rights to use, copy, modify, merge
, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
 substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
DEALINGS IN THE SOFTWARE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 30-Apr-14 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;

using Thn;
using Thn.Web;
using Thn.Web.Mvc;
#endregion

namespace Thn.Web.Blade
{
    /// <summary>
    /// A custom module that supports BLADE features.
    /// </summary>
#if DEBUG || TESTING
    public
#else
    public
#endif
 class BladeModule : IWebServerModule
    {
        #region Fields
        #endregion

        #region Properties

        #region Root View Path
        private string mRootViewPath = "Views";
        /// <summary>
        /// Gets or sets physical path to root of all views.
        /// </summary>
        public string RootViewPath
        {
            get { return mRootViewPath; }
            set { mRootViewPath = value; }
        }
        #endregion

        #region View Path Translator
        private IPathTranslator mViewPathTranslator;
        /// <summary>
        /// Gets or sets the translator responsible for resolving path to views.
        /// </summary>
        public IPathTranslator ViewPathTranslator
        {
            get { return mViewPathTranslator; }
            set { mViewPathTranslator = value; }
        }
        #endregion

        #region View Renderer
        private IViewRenderer mViewRenderer;
        /// <summary>
        /// Gets or sets the renderer responsible for rendering views.
        /// </summary>
        public IViewRenderer ViewRenderer
        {
            get { return mViewRenderer; }
            set { mViewRenderer = value; }
        }
        #endregion

        #region Controller Manager
        private IControllerManager mControllerManager;
        /// <summary>
        /// Gets or sets the view controller manager.
        /// </summary>
        public IControllerManager ControllerManager
        {
            get { return mControllerManager; }
            set { mControllerManager = value; }
        }
        #endregion

        #endregion

        #region IWebServerModule Members

        #region Initialize
        /// <summary>
        /// Configures and starts this module for the provided target.
        /// </summary>
        /// <param name="target">The web server which this modules will run in.</param>
        void IWebServerModule.Initialize(WebServer target)
        {
            //get renderer if not specifically assigned
            if (mViewRenderer == null) mViewRenderer = Services.Get<IViewRenderer>();

            //create path translator if not specifically assigned
            if (mViewPathTranslator == null)
            {
                UriToLocalPathTranslator translator = new UriToLocalPathTranslator();
                translator.Root = mRootViewPath;
                mViewPathTranslator = translator;
                mViewRenderer.PathTranslator = translator;
            }

            //create controller manager if not assigned
            if (mControllerManager == null)
            {
                ControllerManager man = new ControllerManager();
                man.Renderer = mViewRenderer;
                mControllerManager = man;
            }

            //register MVC handler as the very first handler
            MvcRequestHandler handler = new MvcRequestHandler();
            handler.ControllerManager = mControllerManager;
            target.RequestHandlers.Insert(0, handler);
            //target.RequestHandlers.Add(handler);            

            //register blade file handler
            var bladeFileHandler = new BladeFileRequestHandler();
            bladeFileHandler.ViewRenderer = mViewRenderer;
            target.RequestHandlers.Insert(1, bladeFileHandler);
        }
        #endregion

        #endregion

        #region Constructors
        /// <summary>
        /// Create a new instance.
        /// </summary>
        public BladeModule()
            : base()
        { }
        #endregion
    }
}
